//=============================================================================
// filename: input.cpp
// description: Input functions to save keyboard and mouse state to arrays
// autor: Jay Tomten (jaytomten@gmail.com)
//=============================================================================

#include <iostream>
#include "input.h"

//=============================================================================
// constructor
Input::Input()
    {
    // nothing to do
    }

//=============================================================================
// destructor
Input::~Input()
    {
    // nothing to delete
    }

//=============================================================================
// Init() - set up a players input
void Input::Init(int type)
    {

    cout << "Input initialization... ";

    // yes we want to use joysticks
    SDL_JoystickEventState(SDL_ENABLE);
    int joysticks = SDL_NumJoysticks();
    cout << joysticks << " joysticks found.\n";
    if(joysticks > 0)
        {
        joy1 = SDL_JoystickOpen(0);
        cout << SDL_JoystickName(0) << " joystick enabled.\n";
        }
    if(joysticks > 1)
        {
        joy2 = SDL_JoystickOpen(1);
        cout << SDL_JoystickName(1) << " joystick enabled.\n";
        cout << "No more joysticks can be enabled.\n";
        }

    // set states default values
    keyStatePlayer1.up.active =     keyStatePlayer1.up.used =     false;
    keyStatePlayer1.down.active =   keyStatePlayer1.down.used =   false;
    keyStatePlayer1.left.active =   keyStatePlayer1.left.used =   false;
    keyStatePlayer1.right.active =  keyStatePlayer1.right.used =  false;
    keyStatePlayer1.select.active = keyStatePlayer1.select.used = false;
    keyStatePlayer1.cancel.active = keyStatePlayer1.cancel.used = false;

    keyStatePlayer2.up.active =     keyStatePlayer2.up.used =     false;
    keyStatePlayer2.down.active =   keyStatePlayer2.down.used =   false;
    keyStatePlayer2.left.active =   keyStatePlayer2.left.used =   false;
    keyStatePlayer2.right.active =  keyStatePlayer2.right.used =  false;
    keyStatePlayer2.select.active = keyStatePlayer2.select.used = false;
    keyStatePlayer2.cancel.active = keyStatePlayer2.cancel.used = false;

    mouseState.x = 30;
    mouseState.y = 30;
    mouseState.left.active =      false;
    mouseState.right.active =     false;
    mouseState.wheelUp.active =   false;
    mouseState.wheelDown.active = false;
    mouseState.wheelDelta = 0.0f;

    // set up actual keys
    if  (type == INPUT_1P)
        {
        keyStatePlayer1.up.key =     SDLK_UP;
        keyStatePlayer1.down.key =   SDLK_DOWN;
        keyStatePlayer1.left.key =   SDLK_LEFT;
        keyStatePlayer1.right.key =  SDLK_RIGHT;
        keyStatePlayer1.select.key = SDLK_RETURN;
        keyStatePlayer1.cancel.key = SDLK_ESCAPE;
        keyStatePlayer1.escape.key = SDLK_ESCAPE;
        cout << "success.\n";
        }
    else if (type == INPUT_2P)
        {
        keyStatePlayer1.up.key =     SDLK_w;
        keyStatePlayer1.down.key =   SDLK_s;
        keyStatePlayer1.left.key =   SDLK_a;
        keyStatePlayer1.right.key =  SDLK_d;
        keyStatePlayer1.select.key = SDLK_e;
        keyStatePlayer1.cancel.key = SDLK_q;
        keyStatePlayer1.escape.key = SDLK_ESCAPE;
        keyStatePlayer2.up.key =     SDLK_i;
        keyStatePlayer2.down.key =   SDLK_k;
        keyStatePlayer2.left.key =   SDLK_j;
        keyStatePlayer2.right.key =  SDLK_l;
        keyStatePlayer2.select.key = SDLK_o;
        keyStatePlayer2.cancel.key = SDLK_u;
        keyStatePlayer2.escape.key = SDLK_BACKSPACE;
        cout << "success.\n";
        }
    else if(type == INPUT_CUSTOM)
        {
        // load from save file
        cout << "failed: No custom file.\n";
        }
    else cout << "failed.\n";

    }

////////////////////////////////////////////////////////////////
// Update() - update our event structure/vars
bool Input::Update(float d)
    {

    bool alive = true;

    // smooth out the mouse wheel motion
    if(mouseState.wheelUp.active == true)   mouseState.wheelDelta += d;
    if  (mouseState.wheelDown.active == true) mouseState.wheelDelta += d;
    if  (mouseState.wheelDelta >= 0.3)
        {
        mouseState.wheelUp.active = mouseState.wheelDown.active = false;
        mouseState.wheelUp.used =   mouseState.wheelDown.used   = false;
        mouseState.wheelDelta = 0.0f;
        }

    while (SDL_PollEvent(&event) && alive == true)
        {
        switch(event.type)
            {
            case SDL_VIDEORESIZE:
                // send out a call to game class to do this
                // if (y == 0) y = 1;
                // glViewport(0,0,800,600);
                // glMatrixMode(GL_PROJECTION);
                // glLoadIdentity();
                // gluPerspective(45,(float)800/600,0.1,100);
                // glMatrixMode(GL_MODELVIEW);
            case SDL_QUIT:
                alive = false;
                break;
            case SDL_JOYBUTTONDOWN:
                if(event.jbutton.button == 0 || event.jbutton.button == 2 || event.jbutton.button == 4) keyStatePlayer1.select.active = true;
                else if(event.jbutton.button == 1 || event.jbutton.button == 3 || event.jbutton.button == 5) keyStatePlayer1.cancel.active = true;
                break;
            case SDL_JOYBUTTONUP:
                if(event.jbutton.button == 0 || event.jbutton.button == 2 || event.jbutton.button == 4) keyStatePlayer1.select.active = keyStatePlayer1.select.used = false;
                else if(event.jbutton.button == 1 || event.jbutton.button == 3 || event.jbutton.button == 5) keyStatePlayer1.cancel.active = keyStatePlayer1.cancel.used = false;
                break;
            case SDL_JOYAXISMOTION:
                if(event.jaxis.value > 10000 )
                    {
                    if(event.jaxis.axis == 0 || event.jaxis.axis == 2 || event.jaxis.axis == 4) keyStatePlayer1.right.active = true;
                    else if(event.jaxis.axis == 1 || event.jaxis.axis == 3 || event.jaxis.axis == 5) keyStatePlayer1.down.active  = true;
                    }
                else if(event.jaxis.value < -10000)
                    {
                    if(event.jaxis.axis == 0 || event.jaxis.axis == 2 || event.jaxis.axis == 4) keyStatePlayer1.left.active = true;
                    else if(event.jaxis.axis == 1 || event.jaxis.axis == 3 || event.jaxis.axis == 5) keyStatePlayer1.up.active  = true;
                    }
                else
                    {
                    if(event.jaxis.axis == 1 || event.jaxis.axis == 3 || event.jaxis.axis == 5)
                        {
                        keyStatePlayer1.up.active =     keyStatePlayer1.up.used =     false;
                        keyStatePlayer1.down.active =   keyStatePlayer1.down.used =   false;
                        }
                    else if(event.jaxis.axis == 0 || event.jaxis.axis == 2 || event.jaxis.axis == 4)
                        {
                        keyStatePlayer1.right.active =  keyStatePlayer1.right.used =  false;
                        keyStatePlayer1.left.active =   keyStatePlayer1.left.used =   false;

                        }
                    }
                break;
            case SDL_KEYDOWN:
                if(event.key.keysym.sym == keyStatePlayer1.up.key)          keyStatePlayer1.up.active =     true;
                else if(event.key.keysym.sym == keyStatePlayer1.down.key)   keyStatePlayer1.down.active =   true;
                else if(event.key.keysym.sym == keyStatePlayer1.left.key)   keyStatePlayer1.left.active =   true;
                else if(event.key.keysym.sym == keyStatePlayer1.right.key)  keyStatePlayer1.right.active =  true;
                else if(event.key.keysym.sym == keyStatePlayer1.select.key) keyStatePlayer1.select.active = true;
                else if(event.key.keysym.sym == keyStatePlayer1.cancel.key) keyStatePlayer1.cancel.active = true;
                if(event.key.keysym.sym == keyStatePlayer1.escape.key) keyStatePlayer1.escape.active = true;

                else if(event.key.keysym.sym == keyStatePlayer2.up.key)     keyStatePlayer2.up.active =     true;
                else if(event.key.keysym.sym == keyStatePlayer2.down.key)   keyStatePlayer2.down.active =   true;
                else if(event.key.keysym.sym == keyStatePlayer2.left.key)   keyStatePlayer2.left.active =   true;
                else if(event.key.keysym.sym == keyStatePlayer2.right.key)  keyStatePlayer2.right.active =  true;
                else if(event.key.keysym.sym == keyStatePlayer2.select.key) keyStatePlayer2.select.active = true;
                else if(event.key.keysym.sym == keyStatePlayer2.cancel.key) keyStatePlayer2.cancel.active = true;
                if(event.key.keysym.sym == keyStatePlayer2.escape.key) keyStatePlayer2.escape.active = true;

                break;
            case SDL_KEYUP:
                if(event.key.keysym.sym == keyStatePlayer1.up.key)          keyStatePlayer1.up.active =     keyStatePlayer1.up.used =     false;
                else if(event.key.keysym.sym == keyStatePlayer1.down.key)   keyStatePlayer1.down.active =   keyStatePlayer1.down.used =   false;
                else if(event.key.keysym.sym == keyStatePlayer1.left.key)   keyStatePlayer1.left.active =   keyStatePlayer1.left.used =   false;
                else if(event.key.keysym.sym == keyStatePlayer1.right.key)  keyStatePlayer1.right.active =  keyStatePlayer1.right.used =  false;
                else if(event.key.keysym.sym == keyStatePlayer1.select.key) keyStatePlayer1.select.active = keyStatePlayer1.select.used = false;
                else if(event.key.keysym.sym == keyStatePlayer1.cancel.key) keyStatePlayer1.cancel.active = keyStatePlayer1.cancel.used = false;
                if(event.key.keysym.sym == keyStatePlayer1.escape.key) keyStatePlayer1.escape.active = keyStatePlayer1.escape.used = false;

                else if(event.key.keysym.sym == keyStatePlayer2.up.key)     keyStatePlayer2.up.active =     keyStatePlayer2.up.used =     false;
                else if(event.key.keysym.sym == keyStatePlayer2.down.key)   keyStatePlayer2.down.active =   keyStatePlayer2.down.used =   false;
                else if(event.key.keysym.sym == keyStatePlayer2.left.key)   keyStatePlayer2.left.active =   keyStatePlayer2.left.used =   false;
                else if(event.key.keysym.sym == keyStatePlayer2.right.key)  keyStatePlayer2.right.active =  keyStatePlayer2.right.used =  false;
                else if(event.key.keysym.sym == keyStatePlayer2.select.key) keyStatePlayer2.select.active = keyStatePlayer2.select.used = false;
                else if(event.key.keysym.sym == keyStatePlayer2.cancel.key) keyStatePlayer2.cancel.active = keyStatePlayer2.cancel.used = false;
                if(event.key.keysym.sym == keyStatePlayer2.escape.key) keyStatePlayer2.escape.active = keyStatePlayer2.escape.used = false;
                break;
            case SDL_MOUSEMOTION:
                //if(event.motion.xrel > 0.0 || event.motion.xrel < 0.0); // moved left/right
                //if(event.motion.yrel > 0.0 || event.motion.yrel < 0.0); // moved up/verticle
                SDL_GetMouseState(&mouseState.x, &mouseState.y);        // get the actual mouse position
                break;
            case SDL_MOUSEBUTTONDOWN:
                if(event.button.button == SDL_BUTTON_LEFT)
                    {
                    mouseState.left.active =  true;
                    }
                else if(event.button.button == SDL_BUTTON_RIGHT)
                    {
                    mouseState.right.active = true;
                    }
                else if(event.button.button == SDL_BUTTON_WHEELUP)
                    {
                    mouseState.wheelUp.active =   true;
                    mouseState.wheelDown.active = mouseState.wheelDown.used = false;
                    }
                else if(event.button.button == SDL_BUTTON_WHEELDOWN)
                    {
                    mouseState.wheelDown.active = true;
                    mouseState.wheelUp.active = mouseState.wheelUp.used = false;
                    }
                break;
            case SDL_MOUSEBUTTONUP:
                if (event.button.button == SDL_BUTTON_LEFT) mouseState.left.active  = mouseState.left.used  = false;
                else if (event.button.button == SDL_BUTTON_LEFT) mouseState.right.active = mouseState.right.used = false;
                break;
            default:
                alive = true;
                break;
            }
        }
    return alive;

    }



